package cn.yudao.pet.service.impl;

import cn.yudao.org.domain.Employee;
import cn.yudao.pet.domain.PetType;
import cn.yudao.pet.mapper.PetTypeMapper;
import cn.yudao.pet.service.IPetTypeService;
import cn.yudao.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yudao
 * @since 2022-07-16
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> loadTree() {
        List<PetType> allPetType = petTypeMapper.LoadAll();

        HashMap<Long, PetType> longPetTypeHashMap = new HashMap<>();

        for (PetType petType : allPetType) {
            longPetTypeHashMap.put(petType.getId(), petType);
        }

        List<PetType> tree = new ArrayList();

        for (PetType petType : allPetType) {
            if (petType.getPid() == null) {
                tree.add(petType);
            } else {
                PetType petType1 = longPetTypeHashMap.get(petType.getPid());
                if (petType1 != null) {
                    petType1.getChildren().add(petType);
                }
            }
        }
        return tree;
    }

    @Override
    public void save(PetType petType) {
        petTypeMapper.save(petType);
        String dirpath = "";
        if (petType.getPid() != null){
            PetType parent = petTypeMapper.loadById(petType.getPid());
            dirpath = parent.getDirPath() + "/" + petType.getId();
        }else {
            dirpath = "/" + petType.getId();
        }
        petType.setDirPath(dirpath);
        petTypeMapper.upadte(petType);
    }

    @Override
    public void upadte(PetType petType) {
        String dirpath = "";
        Long pid = petType.getPid();
        if (pid != null){
            PetType parent = petTypeMapper.loadById(pid);
            dirpath = parent.getDirPath() + "/" + petType.getId();
        }else {
            dirpath = "/" + petType.getId();
        }
        petType.setDirPath(dirpath);
        petTypeMapper.upadte(petType);
        // 查询所有的类型
        List<PetType> petTypes = petTypeMapper.LoadAll();

        Long id = petType.getId();

        // 通过查询所有数据中dirpath=当前数据id的对象集合
        List<PetType> byDirpathPetType = petTypeMapper.findByDirpathpetType(id);
        // 遍历
        for (PetType petType2 : byDirpathPetType) {
            // 分割找到的数据的dirPath属性
            String[] split1 = petType2.getDirPath().split("/" + id);
            String dirpp = "";
            // 拼接为找到的新的dirpath地址
            dirpp = dirpath + split1[1];
            // 设置值
            petType2.setDirPath(dirpp);
            // 修改查询到的数据
            petTypeMapper.upadte(petType2);
        }

    }

    @Override
    public void removeById(Long id) {
        PetType petType = petTypeMapper.loadById(id);
        Long pid = petType.getPid();
        // 通过查询所有数据中dirpath=当前数据id的对象集合
        List<PetType> byDirpathPetType = petTypeMapper.findByDirpathpetType(id);
        // 遍历
        for (PetType petType2 : byDirpathPetType) {
            // 分割找到的数据的dirPath属性
            String[] split1 = petType2.getDirPath().split("/" + id);
            String dirpp = "";
            // 拼接为找到的新的dirpath地址
            dirpp = split1[0] + split1[1];
            System.out.println(dirpp);
            // 设置dirPath值
            petType2.setDirPath(dirpp);
            // 判断查询到的pettype对象的上级是不是当前的对象
            if (petType2.getPid() == id){
                // 设置pid的值
                petType2.setPid(pid);
            }
            // 修改查询到的数据
            petTypeMapper.upadte(petType2);
        }

        petTypeMapper.removeById(id);
    }
}
